$(function(){
	$.ajax({
		url: '/admin/getfolders/',
		type:'post',
		success:function(data)
		{
			createPathContainer(data);
		}
	});
	$('#cancelFotoB')
	.click(function(){
		$('#flashcontainer').toggle();
		$('#contentcontainer').toggle();
		
	});
});

function createElement(data)
{
	var content=$('<div class=elements></div>');
	for(key in data)
	{
		$(content).append('<div class=baseelement sid='+key+'><span>'+data[key].name+'</span></div><div style="display:none;" class=" mli listCont'+key+'"></div>');
		for(key2 in data[key].service)
		{
			$('.listCont'+key,content).append("<div class=servElement mid="+key2+"><span>"+data[key].service[key2]+"</span></div>");
		}
	}
	$('.baseelement',content).click(function(){
		$('.mli',content).slideUp(250);
		$('.listCont'+$(this).attr('sid'),content).slideDown(550);
	});
	$('.servElement',content).click(function(){
		loadMiniFotos($(this).attr('mid'));
	});
	return content;
}

function createPathContainer(data)
{
	$('#pathcontainer').append(createElement(data));
}

function loadMiniFotos(id)
{
	$.ajax({
		url: '/admin/getfotos/'+id,
		type:'post',
		success:function(data)
		{
			addFotos(data,id);
		}
	});
}

function flashReady() 
{
	
     	var fl=thisMovie("example");
	
	if(fl!=false)
	{
		
		fl.select_js($('#contentcontainer').attr('servid'));
	}

}
function getCookie(name) {
    var matches = document.cookie.match(new RegExp(
      "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
    ))
    return matches ? decodeURIComponent(matches[1]) : undefined
}

function thisMovie(movieName) {
var movie=false;    
if (navigator.appName.indexOf("Microsoft") != -1) {
        
        if (typeof (window[movieName].select_js) == 'function') {
            // < IE9
            movie = window[movieName];
        }
        else if (typeof (document[movieName].select_js) == 'function') {
            // >= IE9
            movie = document[movieName];
        }
    }
    else {
        // NON IE
        movie = document[movieName];
    }

    return movie;
}

function callExternalInterface() {

	$('#flashcontainer').toggle();
	$('#contentcontainer').toggle();  
}

function addFotos(data,id)
{
	var i=0;
	var cont=$('#contentcontainer');
	//console.log(data);
	$(cont)
	.empty()
	.attr('servId',id)
	.append("<div><input type=button value='Загрузить' id='loadFotoB' onclick='callExternalInterface()'></div>");	
	for (key in data)
	{
		$(cont).append('<div class=foto value="'+data[key]['id']+'">' +
				'<div style="background: url('+data[key]['url']+') no-repeat center;">' +
				'<div class="editable delete_btn" value="'+data[key]['id']+'"></div>' +
				'<span class="editable">'+data[key]['name']+'</span>' +
				'</div>' +
			'</div>');
	}
	$('.foto').mouseenter(function(){
		$('.editable',this).fadeIn(300);
	})
	.mouseleave(function(){
		$('.editable',this).toggle();
	})
	.find('.editable')
	.css('display','none')
	.filter('.delete_btn')
	.click(function(){
		if(confirm(askdelete))
		{
			var temp=$(this).parent().parent('.foto');
			$.ajax({
				url: '/admin/deletefotos/'+$(this).attr('value'),
				type:'post',
				success:function(data)
				{
					//loadMiniFotos(id);
				
					if(data.state=='true')
					{
						$(temp).css('display','none');
					}
					else
					{
						alert('Some error in the server. Please try later');
					}
				}
			});
		}
	});
	
	$('span.editable')
	.live("click", function(){
		var temp=$(this).text();
		$('.foto').unbind('mouseleave').unbind('mouseenter');
		$(this).replaceWith("<input type=text class='editable' value='"+temp+"'>");
		$('input.editable').focus().keydown(function(e){
			if(e.keyCode == '13')
			{
				//console.log($(this).parent().parent('.foto').attr('value'));
				$.ajax({
					url: '/admin/editfotos/'+$(this).parent().parent('.foto').attr('value'),
					type:'post',
					data:'name='+$(this).val()
				});
				$(this).replaceWith('<span class="editable">'+$(this).val()+'</span>')
				$('.foto').mouseenter(function(){
					$('.editable',this).fadeIn(300);
				})
				.mouseleave(function(){
					$('.editable',this).toggle();
				});
			}
			
		});
	});
}

function loadedas3(flag)
{
	if(flag=='true')
	{
		$('#flashcontainer').toggle();
		$('#contentcontainer').toggle();
		loadMiniFotos($('#contentcontainer').attr('servId'));
	}
}